package cn.heiliu.selenium.service.impl;

import cn.heiliu.selenium.config.SetupConfig;
import cn.heiliu.selenium.service.IWebDriverCreateService;
import cn.heiliu.selenium.service.IWebDriverService;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.io.File;

/**
 * @author wangyelou
 * @date 2021/6/14
 */
@Slf4j
public class FirefoxWebDriverCreateServiceImpl implements IWebDriverCreateService {

    private static final String GECKO_PATH = "webdriver.gecko.driver";
    private String uid;

    @Override
    public IWebDriverService createWebDriver(SetupConfig setupConfig) {
        // 生产唯一uid
        uid = IdUtil.fastSimpleUUID();

        FirefoxOptions surrogate = getDefaultOption(setupConfig);

        // 启动
        IWebDriverService webDriver = new FirefoxWebDriverServiceImpl(surrogate, setupConfig, uid);

        // 代理认证
        webDriver.installExt(new File(setupConfig.getProxyAuthExt()).toPath());

        log.info("create firefox[{}]", webDriver.getFlag());
        return webDriver;
    }

    /**
     * 默认配置设置
     * @param setupConfig
     * @return
     */
    private FirefoxOptions getDefaultOption(SetupConfig setupConfig) {
        // 注入环境变量
        if (setupConfig.getEnginePath() != null) {
            System.setProperty(GECKO_PATH, setupConfig.getEnginePath());
        }
        if (setupConfig.getBinPath() != null) {
            System.setProperty(FirefoxDriver.SystemProperty.BROWSER_BINARY, setupConfig.getBinPath());
        }

        FirefoxOptions surrogate = new FirefoxOptions();

        // 禁用pdf.js
        surrogate.addPreference("pdfjs.disabled", true);
        surrogate.addPreference("pdfjs.enabledCache.state", false);

        // 不打开未知类型
        surrogate.addPreference("browser.helperApps.alwaysAsk.force", false);

        // 不弹出警告框
        surrogate.addPreference("browser.download.manager.alertOnEXEopen", false);
        surrogate.addPreference("browser.download.manager.focusWhenStarting", false);
        surrogate.addPreference("browser.download.manager.useWindow", false);
        surrogate.addPreference("browser.download.manager.showAlertOnComplete", true);
        surrogate.addPreference("browser.download.manager.closeWhenDone", false);
        surrogate.addPreference("browser.aboutConfig.showWarning", false);

        // 不联其他网站
        surrogate.addPreference("network.captive-portal-service.enabled", false);

        // 不加载图片
        surrogate.addPreference("permissions.default.image", setupConfig.isLoadImage() ? 1 : 2);

        // 不输出geckodriver 日志
        System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");

        // 自动下载文件类型
        surrogate.addPreference("browser.download.folderList", 2);
        surrogate.addPreference("browser.download.dir", setupConfig.getDownDir(uid));
        surrogate.addPreference("browser.download.useDownloadDir", true);
        surrogate.addPreference("browser.download.manager.showWhenStarting", false);
        surrogate.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/x-001, text/h323, drawing/907, audio/x-mei-aac, audio/aiff, text/asa, text/asp, audio/basic, application/vnd.adobe.workflow, application/x-bmp, application/x-c4t, application/x-cals, application/x-netcdf, application/x-cel, application/x-g4, application/x-cit, application/x-cmx, application/pkix-crl, application/x-csi, application/x-cut, application/x-dbm, application/x-x509-ca-cert, application/x-dib, application/msword, application/x-drw, Model/vnd.dwf, application/x-dwg, application/x-dxf, application/x-emf, application/x-ps, application/x-ebx, image/fax, application/fractals, application/x-frm, application/x-gbr, image/gif, application/x-gp4, application/x-hmr, application/x-hpl, application/x-hrf, text/x-component, image/x-icon, application/x-iff, application/x-igs, application/x-img, application/x-internet-signup, java/*, image/jpeg, application/x-jpg, application/x-laplayer-reg, audio/x-liquid-secure, audio/x-la-lms, application/x-ltr, video/x-mpeg, video/mpeg4, application/x-troff-man, application/msaccess, application/x-shockwave-flash, message/rfc822, audio/mid, application/x-mil, audio/x-musicnet-download, application/x-javascript, audio/mp1, video/mpeg, application/vnd.ms-project, video/mpg, audio/rn-mpeg, image/pnetvue, application/x-out, application/x-pkcs12, application/pkcs7-mime, application/x-pkcs7-certreqresp, application/x-pc5, application/x-pcl, application/pdf, application/vnd.adobe.pdx, application/x-pgl, application/vnd.ms-pki.pko, application/x-plt, application/x-png, application/vnd.ms-powerpoint, application/x-ppt, application/pics-rules, application/x-prt, application/postscript, audio/vnd.rn-realaudio, application/x-ras, application/x-red, application/vnd.rn-realsystem-rjs, application/x-rlc, application/vnd.rn-realmedia, audio/x-pn-realaudio, application/vnd.rn-realmedia-secure, application/vnd.rn-realsystem-rmx, image/vnd.rn-realpix, application/vnd.rn-rsml, video/vnd.rn-realvideo, application/x-sat, application/x-sdw, application/x-slb, drawing/x-slk, application/smil, text/plain, application/futuresplash, application/streamingmedia, application/vnd.ms-pki.stl, application/x-sty, application/x-tg4, image/tiff, drawing/x-top, application/x-icq, text/x-vcard, application/vnd.visio, application/x-vpeg005, application/x-vsd, audio/wav, application/x-wb1, application/x-wb3, application/x-wk4, application/x-wks, audio/x-ms-wma, application/x-wmf, video/x-ms-wmv, application/x-ms-wmz, application/x-wpd, application/vnd.ms-wpl, application/x-wr1, application/x-wrk, application/x-ws, application/vnd.adobe.xdp, application/vnd.adobe.xfd, application/x-xls, application/x-xwd, application/vnd.symbian.install, application/x-x_t, application/vnd.android.package-archive, application/x-301, application/x-906, application/x-a11, application/x-anv, video/x-ms-asf, video/avi, application/x-bot, application/x-c90, application/vnd.ms-pki.seccat, application/x-cdr, application/x-cgm, application/x-cmp, application/x-cot, application/x-dbf, application/x-dbx, application/x-dcx, application/x-dgn, application/x-msdownload, application/x-dwf, application/x-dxb, application/vnd.adobe.edn, application/x-epi, application/vnd.fdf, application/x-, application/x-gl2, application/x-hgl, application/x-hpgl, application/mac-binhex40, application/hta, text/webviewhtml, application/x-icb, application/x-ico, application/x-iphone, video/x-ivf, application/x-jpe, audio/x-liquid-file, application/x-latex, application/x-lbm, audio/mpegurl, application/x-mac, application/x-mdb, application/x-mi, audio/x-musicnet-stream, video/x-sgi-movie, audio/mp2, audio/mp3, video/x-mpg, application/x-mmxp, application/x-nrf, text/x-ms-odc, application/pkcs10, application/x-pkcs7-certificates, application/pkcs7-signature, application/x-pci, application/x-pcx, application/x-pic, application/x-perl, audio/scpls, image/png, application/x-ppm, application/x-pr, application/x-prn, application/x-ptn, text/vnd.rn-realtext3d, application/rat-file, application/vnd.rn-recording, application/x-rgb, application/vnd.rn-realsystem-rjt, application/x-rle, application/vnd.adobe.rmf, application/vnd.rn-realsystem-rmj, application/vnd.rn-rn_music_package, application/vnd.rn-realmedia-vbr, application/vnd.rn-realplayer, audio/x-pn-realaudio-plugin, text/vnd.rn-realtext, application/x-rtf, application/x-sam, application/sdp, application/x-stuffit, application/x-sld, application/x-smk, application/vnd.ms-pki.certstore, application/x-tdf, application/x-tga, application/x-tif, application/x-bittorrent, text/iuls, application/x-vda, application/x-vst, audio/x-ms-wax, application/x-wb2, image/vnd.wap.wbmp, application/x-wk3, application/x-wkq, video/x-ms-wm, application/x-ms-wmd, text/vnd.wap.wml, video/x-ms-wmx, application/x-wp6, application/x-wpg, application/x-wq1, application/x-wri, text/scriptlet, video/x-ms-wvx, application/vnd.adobe.xfdf, application/vnd.ms-excel, application/x-xlw, application/x-x_b, application/vnd.iphone, application/x-silverlight-app, application/msexcel, application/msword, application/zip, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/x-zip-compressed");

        // 是否无头
        if (setupConfig.isHeadless()) {
            surrogate.addArguments("--headless");
        }

        return surrogate;
    }
}
